package com.wiz.weixin.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 过滤编码
 */
public class EncodingFilter implements Filter {

    private static final long serialVersionUID=1L;


    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig=filterConfig;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
        } catch(UnsupportedEncodingException ex) {
            filterConfig.getServletContext().log(ex.getMessage());
        }
        try {
            filterChain.doFilter(request, response);
        } catch(ServletException ex) {
            ex.printStackTrace();
//            logger.error(ex);
            filterConfig.getServletContext().log(ex.getMessage());
        } catch(IOException ex) {
            ex.printStackTrace();
//            logger.error(ex);
            filterConfig.getServletContext().log(ex.getMessage());
        } finally {
        }
    }

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
}
